package com.vip.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vip.eduservice.client.VodClient;
import com.vip.eduservice.entity.EduVideo;
import com.vip.eduservice.mapper.EduVideoMapper;
import com.vip.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author hvip
 * @since 2020-08-21
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;
    // 根据课程id删除小节
    @Override
    public void removeVideoByCourseId(String courseId) {
        // 1.根据课程id查询课程所有的视频id
        QueryWrapper<EduVideo> wrappervideo = new QueryWrapper<>();
        wrappervideo.eq("course_id", courseId);
        wrappervideo.select("video_source_id");
        List<EduVideo> eduVideosList = baseMapper.selectList(wrappervideo);

        List<String> videoIds = new ArrayList<>();
        for (int i = 0; i < eduVideosList.size(); i++) {
            EduVideo eduVideo = eduVideosList.get(i);
            String videoSourceId = eduVideo.getVideoSourceId();
            // 如果不为空
            if (!StringUtils.isEmpty(videoSourceId))
                videoIds.add(videoSourceId);
        }

        // 如果有值
        if (videoIds.size() > 0)
            vodClient.deleteBatch(videoIds);

        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);

        baseMapper.delete(wrapper);

    }
}
